/*
 * Copyright 2004-present the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.security.oauth2.core;

import java.util.stream.Stream;

import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestFactory;

import org.springframework.util.StringUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

/**
 * Tests for {@link ClientAuthenticationMethod}.
 *
 * @author Joe Grandja
 */
public class ClientAuthenticationMethodTests {

	@Test
	public void constructorWhenValueIsNullThenThrowIllegalArgumentException() {
		assertThatIllegalArgumentException().isThrownBy(() -> new ClientAuthenticationMethod(null));
	}

	@Test
	public void getValueWhenAuthenticationMethodClientSecretBasicThenReturnClientSecretBasic() {
		assertThat(ClientAuthenticationMethod.CLIENT_SECRET_BASIC.getValue()).isEqualTo("client_secret_basic");
	}

	@Test
	public void getValueWhenAuthenticationMethodClientSecretPostThenReturnClientSecretPost() {
		assertThat(ClientAuthenticationMethod.CLIENT_SECRET_POST.getValue()).isEqualTo("client_secret_post");
	}

	@Test
	public void getValueWhenAuthenticationMethodClientSecretJwtThenReturnClientSecretJwt() {
		assertThat(ClientAuthenticationMethod.CLIENT_SECRET_JWT.getValue()).isEqualTo("client_secret_jwt");
	}

	@Test
	public void getValueWhenAuthenticationMethodPrivateKeyJwtThenReturnPrivateKeyJwt() {
		assertThat(ClientAuthenticationMethod.PRIVATE_KEY_JWT.getValue()).isEqualTo("private_key_jwt");
	}

	@Test
	public void getValueWhenAuthenticationMethodNoneThenReturnNone() {
		assertThat(ClientAuthenticationMethod.NONE.getValue()).isEqualTo("none");
	}

	@Test
	public void getValueWhenAuthenticationMethodTlsClientAuthThenReturnTlsClientAuth() {
		assertThat(ClientAuthenticationMethod.TLS_CLIENT_AUTH.getValue()).isEqualTo("tls_client_auth");
	}

	@Test
	public void getValueWhenAuthenticationMethodSelfSignedTlsClientAuthThenReturnSelfSignedTlsClientAuth() {
		assertThat(ClientAuthenticationMethod.SELF_SIGNED_TLS_CLIENT_AUTH.getValue())
			.isEqualTo("self_signed_tls_client_auth");
	}

	@Test
	public void valueOfWhenAnyAuthenticationMethodThenConstructs() {
		String string = new String("any");
		ClientAuthenticationMethod method = ClientAuthenticationMethod.valueOf(string);
		assertThat(method.getValue()).isSameAs(string);
	}

	@TestFactory
	Stream<DynamicTest> valueOfWhenMatchesStaticThenReturnsStatic() {
		return Stream.of(ClientAuthenticationMethod.methods())
			.map((method) -> DynamicTest.dynamicTest(testName(method.getValue()),
					() -> assertThat(ClientAuthenticationMethod.valueOf(method.getValue())).isSameAs(method)));
	}

	String testName(String method) {
		String methodName = StringUtils.capitalize(method.replaceAll("_", ""));
		return "valueOfWhen" + methodName + "ThenReturnsStatic" + methodName;
	}

}
